//
//  TestCircleGestureView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/8/8.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestCircleGestureView.h"
#import "TestElementHeaders.h"
#import "MKUITestCircleGestureView.h"

@implementation TestCircleGestureView_DisplayCircleCell
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.indexLabel = [[UILabel alloc] init];
        self.indexLabel.textAlignment = NSTextAlignmentCenter;
        self.indexLabel.textColor = UIColor.blackColor;
        self.indexLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.indexLabel];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.indexLabel.frame = self.bounds;
}
- (void)reloadCell{
    [super reloadCell];
    BOOL selected = self.selected&&!self.isTrackHidden;
    self.layer.borderColor = UIColor.clearColor.CGColor;
    self.backgroundColor = [self.class tintColorWithSelected:selected];
    self.indexLabel.text = @(self.cellIndex).stringValue;
}
+ (UIColor *)tintColorWithSelected:(BOOL)selected{
    UIColor *c = selected?UIColor.systemBlueColor:UIColor.clearColor;
    return c;
}
@end
@implementation TestCircleGestureView_CircleCell1
- (UIColor *)tintColorWithSelected:(BOOL)selected{
    UIColor *c = selected?UIColor.systemBlueColor:UIColor.systemGrayColor;
    return c;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    if(self.selected==selected)return;
    if(animated){
        [UIView animateWithDuration:0.25 animations:^{
            [super setSelected:selected animated:animated];
            [self layoutIfNeeded];
        }];
    }
}
@end
@implementation TestCircleGestureView_CircleCell2
+ (UIColor *)tintColorWithSelected:(BOOL)selected{
    UIColor *c = selected?UIColor.systemRedColor:UIColor.systemGrayColor;
    return c;
}
@end
@implementation TestCircleGestureView_CircleCell3
+ (UIColor *)tintColorWithSelected:(BOOL)selected{
    UIColor *c = selected?UIColor.systemGreenColor:UIColor.systemGrayColor;
    return c;
}
@end


//只进行展示
@interface TestCircleGestureView_DisplayView : MKUIListPinHeadViewBase
@property(nonatomic,strong) MKUITestCircleGestureView *gestureView;
@property(nonatomic,strong) UILabel *infoLabel;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
- (void)setSelectedIndexes:(NSArray *)indexes;
@end
@implementation TestCircleGestureView_DisplayView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.whiteColor;
        self.gestureView = [[MKUITestCircleGestureView alloc] init];
        self.gestureView.circleCellClass = TestCircleGestureView_DisplayCircleCell.class;
        self.gestureView.userInteractionEnabled = NO;
        self.gestureView.lineColor = UIColor.clearColor;
        [self.contentView addSubview:self.gestureView];
        //
        self.infoLabel = [[UILabel alloc] init];
        self.infoLabel.textColor = UIColor.blackColor;
        self.infoLabel.font = [UIFont systemFontOfSize:14];
        self.infoLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.infoLabel];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.gestureView,self.infoLabel] constraintParam:(MKUIFlowLayoutConstraintParam_V_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(10) interitemSpacing:10];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (void)setSelectedIndexes:(NSArray *)indexes{
    self.gestureView.selectedCircleIndexes = indexes;
    self.infoLabel.text = [NSString stringWithFormat:@"手势序列为:%@,转弯数为:%ld",[indexes componentsJoinedByString:@"-"],[MKUITestCircleGestureView turnsCountWithSelectedCircleIndexes:indexes cols:self.gestureView.cols]];
    [self mk_reloadInListView];
}
@end
//进行手势操作
@interface TestCircleGestureView_OperationView : MKUIListPinHeadViewBase
@property(nonatomic,strong) MKUITestCircleGestureView *gestureView;
@end
@implementation TestCircleGestureView_OperationView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.whiteColor;
        self.gestureView = [[MKUITestCircleGestureView alloc] init];
        self.gestureView.circleCellClass = TestCircleGestureView_CircleCell1.class;
        [self.contentView addSubview:self.gestureView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.gestureView.frame = bounds;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.gestureView sizeThatFits:size];
}
- (CGFloat)topMargginOfPinning{
    CGFloat marrgin = 0;
    MKUIListView *listView = self.mk_listView;
    NSArray *beforeViews = [listView viewsBeforeItemAtIndexPath:[listView indexPathForCell:self]];
    for(UIView *v in beforeViews){
        if(![v isKindOfClass:MKUIListPinHeadViewBase.class])continue;
        marrgin += v.bounds.size.height;
    }
    return marrgin;
}
@end



@interface TestCircleGestureView ()<MKUITestCircleGestureViewDelegate>
@property(nonatomic,strong) TestCircleGestureView_DisplayView *gestureDisplayView;
@property(nonatomic,strong) TestCircleGestureView_OperationView *gestureOperatonView;
@property(nonatomic,strong) NSArray<NSNumber *> *gestureIndexes;
@end

@implementation TestCircleGestureView
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"测试手势解锁" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadData];
}
- (void)reloadData{
    [self.listView deleteAllSections];
    @MK_WEAKIFY(self);
    
    //
    [self.listView addSection];
    self.gestureDisplayView = [[TestCircleGestureView_DisplayView alloc] init];
    [self.listView addItem:self.gestureDisplayView];
    //
    [self.listView addSection];
    self.gestureOperatonView = [[TestCircleGestureView_OperationView alloc] init];
    self.gestureOperatonView.gestureView.delegate = self;
    [self.listView addItem:self.gestureOperatonView];
    //
    [self.listView addSection];
    MKUITestCircleGestureView *gestureView = self.gestureOperatonView.gestureView;
    //
    [self.listView addItem:[MKUIElementListViewCellSelection testUIColorElementWithTitle:@"backgroundColor" selected:gestureView.backgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
        gestureView.backgroundColor = selectedValue;
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"contentInsets" value:gestureView.contentInsets minimumValue:UIEdgeInsetsZero maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
        @MK_NORMALIZEANDNOTNIL(self);
        gestureView.contentInsets = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"circleInteritemSpacing" value:gestureView.circleInteritemSpacing minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        gestureView.circleInteritemSpacing = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"minCircleInteritemSpacing" value:gestureView.minCircleInteritemSpacing minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        gestureView.minCircleInteritemSpacing = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"circleRadius" value:gestureView.circleRadius minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        gestureView.circleRadius = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"minCircleRadius" value:gestureView.minCircleRadius minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        gestureView.minCircleRadius = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"lineWidth" value:gestureView.lineWidth minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        gestureView.lineWidth = value;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"isTrackHidden" on:gestureView.isTrackHidden whenChange:^(BOOL on) {
        gestureView.isTrackHidden = on;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellSelection testUIColorElementWithTitle:@"lineColor" selected:gestureView.lineColor whenDone:^(UIColor * _Nonnull selectedValue) {
        gestureView.lineColor = selectedValue;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"cleanSelectionWhenTouchBegin" on:gestureView.clearSelectionWhenTouchBegin whenChange:^(BOOL on) {
        gestureView.clearSelectionWhenTouchBegin = on;
        [self.gestureOperatonView mk_reloadInListView];
    }]];
    //
    [self.listView addSection];
    [self.listView addItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"切换圆圈样式" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
        gestureView.circleCellClass = gestureView.circleCellClass==TestCircleGestureView_CircleCell1.class?TestCircleGestureView_CircleCell2.class:TestCircleGestureView_CircleCell1.class;
    }]];
    [self.listView addItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"清空" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
        gestureView.selectedCircleIndexes = nil;
        [self.gestureDisplayView setSelectedIndexes:nil];
    }]];
    
    [self.listView reloadData];
}

- (void)circleGestureViewWillBeginTouching:(MKUITestCircleGestureView *)circleGestureView{
    self.listView.scrollEnabled = NO;
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_setCircleCellClassDefault) object:nil];
    self.gestureOperatonView.gestureView.circleCellClass = TestCircleGestureView_CircleCell1.class;
}
- (void)circleGestureViewDidEndTouching:(MKUITestCircleGestureView *)circleGestureView{
    self.listView.scrollEnabled = YES;
    if(self.gestureIndexes!=nil){
        BOOL eq = [self.gestureIndexes isEqualToArray:circleGestureView.selectedCircleIndexes];
        if(eq){
            circleGestureView.circleCellClass = TestCircleGestureView_CircleCell3.class;
        }else{
            circleGestureView.circleCellClass = TestCircleGestureView_CircleCell2.class;
        }
        //N秒后
        [self performSelector:@selector(_setCircleCellClassDefault) withObject:nil afterDelay:3];
    }
    self.gestureIndexes = circleGestureView.selectedCircleIndexes;
}
- (void)_setCircleCellClassDefault{
    self.gestureIndexes = nil;
    self.gestureOperatonView.gestureView.circleCellClass = TestCircleGestureView_CircleCell1.class;
    self.gestureOperatonView.gestureView.selectedCircleIndexes = nil;
}
- (void)circleGestureView:(MKUITestCircleGestureView *)circleGestureView didSelectedCircleIndexes:(NSArray<NSNumber *> *)selectedCircleIndexes;{
    [self.gestureDisplayView setSelectedIndexes:selectedCircleIndexes];
}

@end
